package com.zy.imagineCommunity.util;

import org.commonmark.node.*;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;

import java.util.regex.Pattern;

/**
 * Makerdwon转变成Html
 */
public class MarkdownUtils {

    /**
     * markdown格式装换成Html格式
     *
     * @param markdown
     * @return
     */
    public static String markdownToHtml(String markdown) {
        Parser parser = Parser.builder().build();
        Node document = parser.parse(markdown);
        HtmlRenderer renderer = HtmlRenderer.builder().build();
        return renderer.render(document);
    }

    /**
     * html转文本
     *
     * @param strHtml
     * @return
     */
    public static String HtmlToText(String strHtml) {
        //剔出<html>的标签
        String txtcontent = strHtml.replaceAll("</?[^>]+>", "");
        //去除字符串中的空格,回车,换行符,制表符
        txtcontent = txtcontent.replaceAll("<a>\\s*|\t|\r|\n</a>", "");

        txtcontent = txtcontent.replaceAll("&quot;", "");

        return txtcontent;
    }

}
